package com.example.dbcms.controller.common;

import com.example.dbcms.domain.FeeStatus;
import com.example.dbcms.domain.User;
import com.example.dbcms.service.FeeStatusService;
import com.example.dbcms.service.UserService;
import com.example.dbcms.utils.ResultVoUtil;
import com.example.dbcms.vo.ResultVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@Api("所有用户登录controller")
public class LoginController {

    /**
     * 注入userService
     */
    @Autowired
    private UserService userService;

    @Autowired
    private FeeStatusService feeStatusService;

    /**
     * 访问8002端口默认跳转到登录页面
     *
     * @return
     */
    @GetMapping("/")
    @ApiOperation("登录页面跳转")
    public String login() {
        return "login";
    }

    /**
     * 登录验证,从表单获取的数据经过shiro认证，通过则保存在令牌中
     *
     * @param username
     * @param password
     * @return
     */
    @PostMapping("/login")
    @ResponseBody
    @ApiOperation("点击登录分析表单数据")
    public ResultVo login(String username, String password) {
        // 从SecurityUtils里边创建一个 subject
        Subject subject = SecurityUtils.getSubject();
        // 在认证提交前准备 token（令牌）
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        try {
            subject.login(token);
        } catch (Exception e) {
            return ResultVoUtil.error(e.getMessage());
        }
        if (subject.isAuthenticated()) {
            String userAccount = (String) SecurityUtils.getSubject().getPrincipal();//获得账号
            User user = userService.getUserByUserAccount(userAccount);
            if(user.getUserRoleId()==1){
                return ResultVoUtil.success("/adminIndex");
            }else {
                return ResultVoUtil.success("/userIndex");
            }
        } else {
            token.clear();
            return ResultVoUtil.error("登录失败");
        }
    }

    /**
     * admin登录
     * 验证成功后跳转首页
     *
     * @param modelMap
     * @return
     */
    @ApiOperation("跳转到管理员首页")
    @GetMapping("/adminIndex")
    public String adminIndex(ModelMap modelMap) {
        String userAccount = (String) SecurityUtils.getSubject().getPrincipal();//获得账号
        modelMap.put("user", userService.getUserByUserAccount(userAccount));
        return "admin/index";
    }
    /**
     * admin登录
     * 验证成功后跳转首页
     *
     * @param modelMap
     * @return
     */
    @ApiOperation("跳转到普通用户首页")
    @GetMapping("/userIndex")
    public String userIndex(ModelMap modelMap) {
        String userAccount = (String) SecurityUtils.getSubject().getPrincipal();//获得账号
        User user = userService.getUserByUserAccount(userAccount);
        FeeStatus feeStatus = feeStatusService.getFeeStatus(user.getUserId());
        modelMap.put("user", user);
        modelMap.put("feeStatus", feeStatus);
        return "user/index";
    }

    /**
     * swagger接口视图
     * @return
     */
    @ApiOperation("swagger接口视图")
    @GetMapping("/admin/swagger")
    public String swagger(){
        return "redirect:/swagger-ui.html";
    }

    /**
     * 身份验证失败访问登录
     *
     * @return
     */
    @GetMapping("/login")
    @ApiOperation("访问login页面")
    public String toLogin() {
        return "login";
    }

    /**
     * 权限认证失败，则跳转到指定页面
     *
     * @return
     */
    @GetMapping("/unauth")
    public String unauth() {
        return "error/unauth";
    }

    /**
     * 退出登录
     *
     * @return
     */
    @ApiOperation("退出登录")
    @GetMapping("/logout")
    public String logout() {
        SecurityUtils.getSubject().logout();
        return "redirect:/";
    }
}
